const gmgz = (a) =>{
  var b;
  var c;
  if(a.length==2){
    b=parseInt(Math.random() * (5.99));
    switch(b){
      case 0:
        c = [[{ x: 1, y: 0 }, { x: 1, y: 1 }, { x: 1, y: 2 }, { x: 1, y: 3 }, { x: 1, y: 4 }, { x: 1, y: 5 }], [{ x: 2, y: 4 }, { x: 3, y: 4 }, { x: 4, y: 4 }, { x: 5, y: 4 }, { x: 6, y: 4 }, { x: 7, y: 4 }]];
        return c;
        case 1:
        c = [[{ x: 1, y: 1 }, { x: 2, y: 1 }, { x: 3, y: 1 }, { x: 4, y: 1 }, { x: 5, y: 1 }, { x: 6, y: 1 }], [{ x: 3, y: 2 }, { x: 3, y: 3 }, { x: 3, y: 4 }, { x: 3, y: 5 }, { x: 3, y: 6 }, { x: 3, y: 7 }]];
        return c;
        case 2:
        c = [[{ x: 1, y: 1 }, { x: 2, y: 1 }, { x: 3, y: 1 }, { x: 4, y: 1 }, { x: 5, y: 1 }, { x: 6, y: 1 }], [{ x: 7, y: 2 }, { x: 7, y: 3 }, { x: 7, y: 4 }, { x: 7, y: 5 }, { x: 7, y: 6 }, { x: 7, y: 7 }]];
        return c;
        case 3:
        c = [[{ x: 0, y: 0 }, { x: 1, y: 1 }, { x: 2, y: 2 }, { x: 3, y: 3 }, { x: 4, y: 4 }, { x: 5, y: 5 }], [{ x: 6, y: 1 }, { x: 6, y: 2 }, { x: 6, y: 3 }, { x: 6, y: 4 }, { x: 6, y: 5 }, { x: 6, y: 6 }]];
        return c;
        case 4:
        c = [[{ x: 1, y: 1 }, { x: 1, y: 2 }, { x: 1, y: 3 }, { x: 1, y: 4 }, { x: 1, y: 5 }, { x: 1, y: 6 },], [{ x: 6, y: 1 }, { x: 6, y: 2 }, { x: 6, y: 3 }, { x: 6, y: 4 }, { x: 6, y: 5 }, { x: 6, y: 6 }]];
        return c;
        case 5:
        c = [[{ x: 0, y: 6 }, { x: 1, y: 5 }, { x: 2, y: 4 }, { x: 3, y: 3 }, { x: 4, y: 2 }, { x: 5, y: 1 },], [{ x: 2, y: 7 }, { x: 3, y: 7 }, { x: 4, y: 7 }, { x: 5, y: 7 }, { x: 6, y: 7 }, { x: 7, y: 7 }]];
        return c;
    }
  } else if (a.length == 4){
    b = parseInt(Math.random() * (5.99));
    switch (b) {
      case 0:
        c = [[{ x: 1, y: 0 }, { x: 1, y: 1 }, { x: 1, y: 2 }, { x: 1, y: 3 }, { x: 1, y: 4 }, { x: 1, y: 5 }], [{ x: 2, y: 1 }, { x: 3, y: 1 }, { x: 4, y: 1 }, { x: 5, y: 1 }, { x: 6, y: 1 }, { x: 7, y: 1 }], [{ x: 2, y: 3 }, { x: 3, y: 3 }, { x: 4, y: 3 }, { x: 5, y: 3 }, { x: 6, y: 3 }, { x: 7, y: 3 }], [{ x: 2, y: 4 }, { x: 3, y: 4 }, { x: 4, y: 4 }, { x: 5, y: 4 }, { x: 6, y: 4 }, { x: 7, y: 4 }]];
        return c;
        case 1:
        c = [[{ x: 1, y: 1 }, { x: 2, y: 1 }, { x: 3, y: 1 }, { x: 4, y: 1 }, { x: 5, y: 1 }, { x: 6, y: 1 }], [{ x: 3, y: 2 }, { x: 3, y: 3 }, { x: 3, y: 4 }, { x: 3, y: 5 }, { x: 3, y: 6 }, { x: 3, y: 7 }], [{ x: 5, y: 2 }, { x: 5, y: 3 }, { x: 5, y: 4 }, { x: 5, y: 5 }, { x: 5, y: 6 }, { x: 5, y: 7 }], [{ x: 6, y: 2 }, { x: 6, y: 3 }, { x: 6, y: 4 }, { x: 6, y: 5 }, { x: 6, y: 6 }, { x: 6, y: 7 }]];
          return c
          case 2:
        c = [[{ x: 2, y: 0 }, { x: 3, y: 0 }, { x: 4, y: 0 }, { x: 5, y: 0 }, { x: 6, y: 0 }, { x: 7, y: 0 },], [{ x: 2, y: 1 }, { x: 3, y: 1 }, { x: 4, y: 1 }, { x: 5, y: 1 }, { x: 6, y: 1 }, { x: 7, y: 1 },], [{ x: 2, y: 2 }, { x: 3, y: 2 }, { x: 4, y: 2 }, { x: 5, y: 2 }, { x: 6, y: 2 }, { x: 7, y: 2 },], [{ x: 0, y: 2 }, { x: 1, y: 3 }, { x: 2, y: 4 }, { x: 3, y: 5 }, { x: 4, y: 6 }, { x: 5, y: 7 },]];
        return c;
        case 3:
        c = [[{ x: 0, y: 0 }, { x: 0, y: 1 }, { x: 0, y: 2 }, { x: 0, y: 3 }, { x: 0, y: 4 }, { x: 0, y: 5 },], [{ x: 1, y: 2 }, { x: 2, y: 3 }, { x: 3, y: 4 }, { x: 4, y: 5 }, { x: 5, y: 6 }, { x: 6, y: 7 },], [{ x: 2, y: 0 }, { x: 3, y: 0 }, { x: 4, y: 0 }, { x: 5, y: 0 }, { x: 6, y: 0 }, { x: 7, y: 0 },], [{ x: 7, y: 2 }, { x: 7, y: 3 }, { x: 7, y: 4 }, { x: 7, y: 5 }, { x: 7, y: 6 }, { x: 7, y: 7 },]];
          return c;
      case 4:
        c = [[{ x: 0, y: 0 }, { x: 1, y: 1 }, { x: 2, y: 2 }, { x: 3, y: 3 }, { x: 4, y: 4 }, { x: 5, y: 5 },], [{ x: 0, y: 2 }, { x: 0, y: 3 }, { x: 0, y: 4 }, { x: 0, y: 5 }, { x: 0, y: 6 }, { x: 0, y: 7 },], [{ x: 2, y: 0 }, { x: 3, y: 0 }, { x: 4, y: 0 }, { x: 5, y: 0 }, { x: 6, y: 0 }, { x: 7, y: 0 },], [{ x: 7, y: 2 }, { x: 7, y: 3 }, { x: 7, y: 4 }, { x: 7, y: 5 }, { x: 7, y: 6 }, { x: 7, y: 7 },]];
          return c;
       case 5:
        c = [[{ x: 0, y: 0 }, { x: 0, y: 1 }, { x: 0, y: 2 }, { x: 0, y: 3 }, { x: 0, y: 4 }, { x: 0, y: 5 },], [{ x: 1, y: 2 }, { x: 1, y: 3 }, { x: 1, y: 4 }, { x: 1, y: 5 }, { x: 1, y: 6 }, { x: 1, y: 7 },], [{ x: 2, y: 1 }, { x: 3, y: 1 }, { x: 4, y: 1 }, { x: 5, y: 1 }, { x: 6, y: 1 }, { x: 7, y: 1 },], [{ x: 4, y: 2 }, { x: 4, y: 3 }, { x: 4, y: 4 }, { x: 4, y: 5 }, { x: 4, y: 6 }, { x: 4, y: 7 },]];
        return c;
    }
  } else if (a.length==6){
    b = parseInt(Math.random() * (2.99));
    switch (b) {
      case 0:
        c = [[{ x: 2, y: 0 }, { x: 3, y: 0 }, { x: 4, y: 0 }, { x: 5, y: 0 }, { x: 6, y: 0 }, { x: 7, y: 0 }], [{ x: 2, y: 1 }, { x: 3, y: 1 }, { x: 4, y: 1 }, { x: 5, y: 1 }, { x: 6, y: 1 }, { x: 7, y: 0 }], [{ x: 0, y: 1 }, { x: 0, y: 2 }, { x: 0, y: 3 }, { x: 0, y: 4 }, { x: 0, y: 5 }, { x: 0, y: 6 }], [{ x: 1, y: 2 }, { x: 1, y: 3 }, { x: 1, y: 4 }, { x: 1, y: 5 }, { x: 1, y: 6 }, { x: 1, y: 7 }], [{ x: 2, y: 3 }, { x: 3, y: 3 }, { x: 4, y: 3 }, { x: 5, y: 3 }, { x: 6, y: 3 }, { x: 7, y: 3 }], [{ x: 2, y: 4 }, { x: 3, y: 4 }, { x: 4, y: 4 }, { x: 5, y: 4 }, { x: 6, y: 4 }, { x: 7, y: 4 }]]
        return c;
        case 1:
        c = [[{ x: 1, y: 0 }, { x: 2, y: 0 }, { x: 3, y: 0 }, { x: 4, y: 0 }, { x: 5, y: 0 }, { x: 6, y: 0 }], [{ x: 0, y: 1 }, { x: 1, y: 1 }, { x: 2, y: 1 }, { x: 3, y: 1 }, { x: 4, y: 1 }, { x: 5, y: 1 }], [{ x: 1, y: 3 }, { x: 2, y: 3 }, { x: 3, y: 3 }, { x: 4, y: 3 }, { x: 5, y: 3 }, { x: 6, y: 3 }], [{ x: 2, y: 4 }, { x: 3, y: 4 }, { x: 4, y: 4 }, { x: 5, y: 4 }, { x: 6, y: 4 }, { x: 7, y: 4 }], [{ x: 0, y: 5 }, { x: 1, y: 5 }, { x: 2, y: 5 }, { x: 3, y: 5 }, { x: 4, y: 5 }, { x: 5, y: 5 }], [{ x: 2, y: 6 }, { x: 3, y: 6 }, { x: 4, y: 6 }, { x: 5, y: 6 }, { x: 6, y: 6 }, { x: 7, y: 6 }]];
          return c;
          case 2:
        c = [[{ x: 0, y: 0 }, { x: 0, y: 1 }, { x: 0, y: 2 }, { x: 0, y: 3 }, { x: 0, y: 4 }, { x: 0, y: 5 }], [{ x: 2, y: 1 }, { x: 2, y: 2 }, { x: 2, y: 3 }, { x: 2, y: 4 }, { x: 2, y: 5 }, { x: 2, y: 6 }], [{ x: 4, y: 1 }, { x: 4, y: 2 }, { x: 4, y: 3 }, { x: 4, y: 4 }, { x: 4, y: 5 }, { x: 4, y: 6 }], [{ x: 5, y: 1 }, { x: 5, y: 2 }, { x: 5, y: 3 }, { x: 5, y: 4 }, { x: 5, y: 5 }, { x: 5, y: 6 }], [{ x: 6, y: 1 }, { x: 6, y: 2 }, { x: 6, y: 3 }, { x: 6, y: 4 }, { x: 6, y: 5 }, { x: 6, y: 6 }], [{ x: 7, y: 1 }, { x: 7, y: 2 }, { x: 7, y: 3 }, { x: 7, y: 4 }, { x: 7, y: 5 }, { x: 7, y: 6 }]];
        return c;

    }
  }
  var c = [[{ x: 2, y: 0 }, { x: 3, y: 0 }, { x: 4, y: 0 }, { x: 5, y: 0 }, { x: 6, y: 0 }, { x: 7, y: 0 }], [{ x: 2, y: 1 }, { x: 3, y: 1 }, { x: 4, y: 1 }, { x: 5, y: 1 }, { x: 6, y: 1 }, { x: 7, y: 0 }], [{ x: 0, y: 1 }, { x: 0, y: 2 }, { x: 0, y: 3 }, { x: 0, y: 4 }, { x: 0, y: 5 }, { x: 0, y: 6 }], [{ x: 1, y: 2 }, { x: 1, y: 3 }, { x: 1, y: 4 }, { x: 1, y: 5 }, { x: 1, y: 6 }, { x: 1, y: 7 }], [{ x: 2, y: 3 }, { x: 3, y: 3 }, { x: 4, y: 3 }, { x: 5, y: 3 }, { x: 6, y: 3 }, { x: 7, y: 3 }], [{ x: 2, y: 4 }, { x: 3, y: 4 }, { x: 4, y: 4 }, { x: 5, y: 4 }, { x: 6, y: 4 }, { x: 7, y: 4 }]];
  return c;
}
//设置数组数据
const gmsetArray = (name, i, j) => {
  return "" + name + "[" + i + "][" + j + "]";
}
//游戏初始化
const gamestart = (a) =>{
  //var b = gmgz();
  var a1 = gmsetList1(a);
  var a2 = gmsetList2(a.data.showShuffle,a1);
  return a2;
}
//解析接收题目为多条
const gmsetList1 = (a) => {
  var b = new Array();
  var c = new Array();
  for (let i=0;i<a.data.originalText.length;i++){
    b = a.data.originalText[i].split(',');
    for(let j =0;j<b.length;j++){
      c.push(b[j]);
    }
  }
  console.log("gmsetlist1"+c);
  //var d = gmsetList2(c);
  return c;
}
//题目拆封为顺序或乱序
const gmsetList2 = (show,a) =>{
  var b = [];
  var c = [];
  var j = 0;
  var x = "";
  var y = gmgz(a);
  var z = [];
  console.log("show="+show);
  for(let i =0;i<a.length;i++){
    console.log("gmsetlist2" +a);
    b = a[i].split('');
    j=0;
    if (show == 2){
      while (b.length > 0) {
        var index = parseInt(Math.random() * (b.length - 0.01));
        x = { text: b[index], px: y[i][j].x, py: y[i][j].y }
        z.push(x);
        b.splice(index, 1);
        j++;
      }
      c.push(z);
      z = [];
    }else{
      while (b.length > 0) {
        var index = 0;
        x = { text: b[index], px: y[i][j].x, py: y[i][j].y }
        z.push(x);
        b.splice(index, 1);
        j++;
      }
      c.push(z);
      z = [];
    }
  }
  return c;
}

const gmsetList3 = (a) => {
  var b = [];
  var c = [];
  let j = 0;
  var x = "";
  var y = gmgz();
  var z = [];
  for (let i = 0; i < a.length; i++) {
    b = a[i].split('');
    j = 0;
    while (b.length > 0) {
      var index = parseInt(Math.random() * (b.length - 1));
      x = { text: b[index], px: y[i][j].x, py: y[i][j].y }
      z.push(x);
      b.splice(index, 1);
      j++;
    }
    c.push(z);
    z = [];
  }
  return c;
}
//设置文字播放
const gmgoplay = (a,b)=>{
  var index= new Array();
  var c = new Array();
  var d = new Array();
  var arr = new Array();
  var arr1 = new Array();
  for (let i = 0; i < b.data.originalText.length; i++) {
    index = b.data.originalText[i].split(',');
    for (let j=0; j < index.length; j++) {
      c.push(index[j]);
    }
  }
  for (let i = 0;i<c.length;i++){
    d = c[i].split('');
    for (let j=0;j<d.length;j++){
      arr.push(d[j]);
    }
  }
  var goj = 0;
  for (let i = 0;i<a.length;i++){
    for (goj = 0; goj < a[i].length; goj++){
      if (a[i][goj].text == arr[0]){
        arr1.push({ text: a[i][goj].text, px: a[i][goj].px, py: a[i][goj].py});
        arr.splice(0, 1);
        a[i].splice(goj,1);
        goj = -1;
      }
    }
    if(arr.length == 0){
      break;
    }
  }
  console.log(arr1);
  return arr1;
}
const gmgoplay1 = (a, b) => {
  var index = new Array();
  var c = new Array();
  var d = new Array();
  var z = [];
  var arr = new Array();
  var arr1 = new Array();
  for (let i = 0; i < b.data.originalText.length; i++) {
    index = b.data.originalText[i].split(',');
    for (let j = 0; j < index.length; j++) {
      c.push(index[j]);
    }
  }
  for (let i = 0; i < c.length; i++) {
    d = c[i].split('');
    for (let j = 0; j < d.length; j++) {
      arr.push(d[j]);
    }
  }
  var goj = 0;
  for (let i = 0; i < a.length; i++) {
    for (goj = 0; goj < a[i].length; goj++) {
      if (a[i][goj].text == arr[0]) {
        z.push({ text: a[i][goj].text, px: a[i][goj].px, py: a[i][goj].py });
        arr.splice(0, 1);
        a[i].splice(goj, 1);
        goj = -1;
      }
    }
    arr1.push(z);
    z=[];
    if (arr.length == 0) {
      break;
    }
  }
  console.log(arr1);
  return arr1;
}


//设置文字降落 正序或乱序
const gmtextdown =(a)=>{
  var a1 =[];
  var b=[];
  var c=[];
  var x;
  
  var d = new Array();
  for (let i = 0; i < a.data.randomText.length; i++) {
    d = a.data.randomText[i].split(',');
    for (let j = 0; j < d.length; j++) {
      a1.push(d[j]);
    }
  }
  console.log("textdown:"+a1);
  for (let i = 0; i <a1.length; i++) {
    console.log("gmsetlist2" + a);
    b = a1[i].split('');
      while (b.length > 0) {
        x = { text: b[0]};
        c.push(b[0]);
        b.splice(0, 1);
      }
    
  }
  
  return c;
}
const gmgettoken = ()=>{
  wx.login({
    success: res => {
      // 发送 res.code 到后台换取 openId, sessionKey, unionId
      if (res.code) {
        //发起网络请求
        wx.request({
          url: 'https://api.wnymeng.com/wxmini/login?code=' + res.code,
          method: 'GET',
          success: function (res) {
            console.log(res)
            getApp().globalData.token = "Bearer "+ res.data.data.key;
            getApp().globalData.session_key = res.data.data.session_key;
            wx.redirectTo({
              url: '../index/index?token=' + getApp().globalData.token
            });
          }
        })
      } else {
        console.log(that.globalData.token + '登录失败！' + res.errMsg)
      }
    }
  })
}

module.exports = {
  gmsetArray: gmsetArray,
  gamestart: gamestart,
  gmgoplay: gmgoplay,
  gmgoplay1: gmgoplay1,
  gmtextdown: gmtextdown,
  gmgettoken: gmgettoken
}